<template>
  <div class="blog-container">
    <!-- 导航栏 -->
    <el-menu class="nav-menu" mode="horizontal" background-color="#ff4455" text-color="#fff"
      active-text-color="#ffeb3b">
      <el-menu-item index="1">首页</el-menu-item>
      <el-menu-item index="2">博文</el-menu-item>
      <el-menu-item index="3">视频</el-menu-item>
      <el-sub-menu index="4" popper-class="nav-submenu">
        <template #title>下载</template>
        <el-menu-item index="4-1">软件下载</el-menu-item>
        <el-menu-item index="4-2">资源下载</el-menu-item>
      </el-sub-menu>
      <el-menu-item index="5">行业动态</el-menu-item>
      <el-menu-item index="6">干货分享</el-menu-item>
      <el-sub-menu index="7" popper-class="nav-submenu">
        <template #title>课程</template>
        <el-menu-item index="7-1">编程课程</el-menu-item>
        <el-menu-item index="7-2">设计课程</el-menu-item>
      </el-sub-menu>
      <el-sub-menu index="8" popper-class="nav-submenu">
        <template #title>店铺</template>
        <el-menu-item index="8-1">数码产品</el-menu-item>
        <el-menu-item index="8-2">周边商品</el-menu-item>
      </el-sub-menu>
      <el-sub-menu index="9" popper-class="nav-submenu">
        <template #title>注册与登录</template>
        <el-menu-item index="9-1" @click="Tzregister">注册</el-menu-item>
        <el-menu-item index="9-2" @click="goToLogin">登录</el-menu-item>
      </el-sub-menu>
      <el-sub-menu index="10">
        <template #title>个人中心</template>
        <el-menu-item index="10-1" @click="ToPersonalCenter">个人信息</el-menu-item>
      </el-sub-menu>
      <!-- <el-menu-item class="nav-search" index="10">
        <el-input placeholder="搜索" prefix-icon="el-icon-search" class="search-input" />
        <el-icon style="vertical-align: middle">
          <Search />
        </el-icon>
      </el-menu-item> -->
      <el-menu-item class="nav-cart" index="11">
        购物车 0 - ¥0.00
        <el-icon class="cart-icon">
          <ShoppingCart />
        </el-icon>
      </el-menu-item>
    </el-menu>

    <!-- 内容区域 -->
    <div class="article-container" style="">
      <div v-for="(articleItem, index) in article" :key="index" class="article-item">
        <div class="article-info">
          <p class="article-author">作者: {{ articleItem.author }}</p>
          <p class="article-time">{{ articleItem.publish_time }}</p>
          <p class="article-title" @click="view">标题: {{ articleItem.title }}</p>
        </div>
        <div class="imges" style="">
          <img :src="articleItem.cover_img" @click="view" alt="">
        </div>
      </div>
    </div>
  </div>

</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ShoppingCart } from '@element-plus/icons-vue';
import request from '@/request';
import { useRouter } from 'vue-router';
const router = useRouter()

// 获取文章数据
let article = ref([]);
onMounted(() => {
  request({
    method: 'GET',
    url: 'users/getarticle/'
  }).then(resp => {
    if (Array.isArray(resp.data.article_list)) {
      article.value = resp.data.article_list;
      // console.log('文章数据:', resp.data.article_list);
    } else {
      console.error('后端返回的article_list不是数组类型');
    }
  }).catch(error => {
    console.log('文章显示失败', error);
  });
});

function view() {
  router.push({ path: '/articlelist' });
}

// 跳转注册
const Tzregister = () => {
  router.push('/register')
}
// 跳转登录
const goToLogin = () => {
  router.push('/login')
}
// 跳转个人中心页面
const ToPersonalCenter = () => {
  router.push('/personalcenter')
}


</script>

<style scoped>
.blog-container {
  font-family: 'Arial', sans-serif;
}

.nav-menu {
  margin-top: 10px;
}

.card-cover {
  width: 300px;
}

.nav-search .search-input {
  width: 200px;
  margin-left: 20px;
}

.nav-cart {
  margin-left: 20px;
}

.cart-icon {
  margin-right: 5px;
}

/* 文章列表区 */
.article-container {
  width: 600px;
  /* position: absolute; */
  /* background-color: pink; */
  padding: 1.5rem 2rem;
  box-sizing: border-box;
  overflow-y: auto;
  z-index: 20;
}

.article-info {
  display: inline-block;
}

.article-item {
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #8a8888;
  transition: all 0.3s ease;
}

.article-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.article-title {
  font-size: 1.1rem;
  font-weight: 500;
  color: #333;
  margin-bottom: 0.5rem;
}

.article-author {
  font-size: 0.9rem;
  color: #888;
  margin-bottom: 0.3rem;
}

.article-time {
  font-size: 0.8rem;
  color: #aaa;
}

.article-stats {
  display: flex;
  gap: 1.5rem;
  font-size: 0.85rem;
  color: #888;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.imges {
  margin-left: 40px;
  display: inline-block;
  width: 120px;
  height: 80px;
  background-color: aqua;
}

.imges img {
  width: 120px;
  height: 80px;
}
</style>